package Leo.ribbon;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.Random;

/**
 * 自定义负载均衡规则
 */
public class LoadBalanceRuleImpl implements IRule {
    ILoadBalancer lb ;

    public LoadBalanceRuleImpl(ILoadBalancer lb){
        this.lb = lb;
    }
    public Server choose(Object o) {
        List<Server> allServers = lb.getAllServers();
        int i = new Random().nextInt(allServers.size());
        System.out.println("随机来一个服务器吧,就来" + i +"好了");
        return allServers.get(i);
    }

    public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
        this.lb = iLoadBalancer;
    }

    public ILoadBalancer getLoadBalancer() {
        return lb;
    }
}
